import { defineStore } from 'pinia'

const useCounterStore = defineStore('counter', {
  state: () => ({
    count: 0
  }),
  getters: {
    oddOrEven: (state) => state.count % 2 === 0 ? '偶数' : '奇数'
    //箭头函数不能使用this.count
    //如果要使用，可以如下改为普通函数
    // oddOrEven(){
    //     return this.count % 2 === 0 ? '偶数' : '奇数'
    // }
  },
  actions: {
    increaseCount(step: number) {
      this.count += step
    },
    decreaseCount(step: number) {
      this.count -= step
    },
    oddIncreaseCount(step: number) {
      if (this.count % 2 === 1) {
        this.count += step
      }
    },
    async asyncIncreaseCount(step: number) {
      await new Promise(resolve => setTimeout(resolve, 1000))
      this.count += step
    }
  }
})

export default useCounterStore